/*
 * @(#)SeparatePage.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.util.common;

import java.io.IOException;
import java.io.Writer;

import org.apache.log4j.Logger;
import org.apache.struts2.components.Component;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * SeparatePage
 * 
 * @author BIS 2012-12-24
 */
public class SeparatePage extends Component
{
    /**
     * Logger
     */
    private static final Logger logger = Logger.getLogger(SeparatePage.class);
    /**
     * currPage
     */
    private String              currPage;
    /**
     * pageSize
     */
    private String              pageSize;
    /**
     * rowCount
     */
    private String              rowCount;
    /**
     * action
     */
    private String              action;

    /**
     * SeparatePage
     * @param arg0
     */
    public SeparatePage(ValueStack arg0)
    {
        super(arg0);
    }

    /**
     * start
     */
    @Override
    public boolean start(Writer writer)
    {
        boolean result = super.start(writer);
        try
        {
            StringBuffer sb_page = new StringBuffer();
            boolean isValid = true;

            //从ValueStack中取出数值  
            if (isValid)
            {
                if (rowCount.startsWith("%{") && rowCount.endsWith("}"))
                {
                    rowCount = rowCount.substring(2, rowCount.length() - 1);
                    rowCount = this.getStack().findValue(rowCount).toString();
                    isValid = rowCount == null ? false : true;
                }
                else
                {
                    isValid = false;
                }
            }
            if (isValid)
            {
                if (currPage.startsWith("%{") && currPage.endsWith("}"))
                {
                    currPage = currPage.substring(2, currPage.length() - 1);
                    currPage = this.getStack().findValue(currPage).toString();
                    isValid = currPage == null ? false : true;
                }
                else
                {
                    isValid = false;
                }
            }
            if (isValid)
            {
                if (pageSize.startsWith("%{") && pageSize.endsWith("}"))
                {
                    pageSize = pageSize.substring(2, pageSize.length() - 1);
                    pageSize = this.getStack().findValue(pageSize).toString();
                    isValid = pageSize == null ? false : true;
                }
                else
                {
                    isValid = false;
                }
            }
            if (isValid)
            {
                if (action.startsWith("%{") && action.endsWith("}"))
                {
                    action = action.substring(2, action.length() - 1);
                    action = (String) this.getStack().findValue(action);
                    isValid = action == null ? false : true;
                }
                else
                {
                    isValid = false;
                }
            }
            logger.debug("isValid: " + isValid);
            if (isValid)
            {
                Integer currPageInt = Integer.valueOf(currPage);
                Integer pageSizeInt = Integer.valueOf(pageSize);
                Integer rowCountInt = Integer.valueOf(rowCount);
                Integer pageCountInt = rowCountInt % pageSizeInt == 0 ? rowCountInt / pageSizeInt : rowCountInt / pageSizeInt + 1;
                logger.debug("currPageInt: " + currPageInt + " pageSizeInt: " + pageSizeInt + " rowCountInt: " + rowCountInt + " pageCountInt: " + pageCountInt);
                if (currPageInt <= 1)
                    currPageInt = 1;
                if (pageCountInt > 0 && currPageInt >= pageCountInt)
                    currPageInt = pageCountInt;

                sb_page.append("<tbody><tr>");
                sb_page.append("<td width=\"10%\">共" + rowCountInt + "条</td>");
                sb_page.append("<td width=\"10%\">第" + currPageInt + "/" + pageCountInt + "页</td>");
                sb_page.append("<td width=\"10%\">每页显示" + pageSizeInt + "条</td>");

                sb_page.append("<td align=\"right\" width=\"40%\">");
                if (currPageInt > 1)
                {
                    sb_page.append("<a href='" + action + "?pageData.currPage=1&pageData.pageSize=" + pageSizeInt + "&pageData.rowCount=" + rowCountInt + "'>首页</a>");
                    sb_page.append("<a  href='" + action + "?pageData.currPage=" + (currPageInt - 1) + "&pageData.pageSize=" + pageSizeInt + "&pageData.rowCount=" + rowCountInt + "'><img src=\"/netcity/images/backend/paging/page-prev.gif\" alt=\"上一页\" name=\"上一页\"></a>");
                }
                else
                {
                    sb_page.append("首页");
                    sb_page.append("<img src=\"/netcity/images/backend/paging/page-prev.gif\" alt=\"上一页\" name=\"上一页\">");
                }

                for (int i = 1; i < pageCountInt + 1; i++)
                {
                    if (currPageInt == i)
                    {
                        sb_page.append("<b>[" + i + "]</b>&nbsp;");
                    }
                    else
                    {
                        sb_page.append("<a href='" + action + "?pageData.currPage=" + i + "&pageData.pageSize=" + pageSizeInt + "&pageData.rowCount=" + rowCountInt + "'>[" + i + "]</a>");
                    }
                }
                if (currPageInt < pageCountInt)
                {
                    sb_page.append("<a href='" + action + "?pageData.currPage=" + (currPageInt + 1) + "&pageData.pageSize=" + pageSizeInt + "&pageData.rowCount=" + rowCountInt + "'><img src=\"/netcity/images/backend/paging/page-next.gif\" alt=\"下一页\" name=\"下一页\"></a>");
                    sb_page.append("<a href='" + action + "?pageData.currPage=" + pageCountInt + "&pageData.pageSize=" + pageSizeInt + "&pageData.rowCount=" + rowCountInt + "'>尾页</a>");
                }
                else
                {
                    sb_page.append("<img src=\"/netcity/images/backend/paging/page-next.gif\" alt=\"下一页\" name=\"下一页\">");
                    sb_page.append("尾页");
                }

                sb_page.append("</td>");
                sb_page.append("<td style=\"padding-right: 10px;\" width=\"30%\" align=\"right\">转到<input maxlength=\"5\" id=\"page\" name=\"page\" style=\"ime-mode: disabled;\" type=\"text\">页");
                sb_page.append("<a href='" + action + "?pageData.currPage='+document.getElementById('page').value+'&pageData.pageSize=" + pageSizeInt + "&pageData.rowCount=" + rowCountInt + "'><img name=\"goPage\" src=\"/netcity/images/backend/paging/page-next.gif\"></a>");
                sb_page.append("&nbsp;</td>");
            }
            logger.debug("start:" + sb_page.toString());
            writer.write(sb_page.toString());

        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        return result;
    }

    /**
     * @return the currPage
     */
    public String getCurrPage()
    {
        return currPage;
    }

    /**
     * @param currPage the currPage to set
     */
    public void setCurrPage(String currPage)
    {
        this.currPage = currPage;
    }

    /**
     * @return the pageSize
     */
    public String getPageSize()
    {
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(String pageSize)
    {
        this.pageSize = pageSize;
    }

    /**
     * @return the rowCount
     */
    public String getRowCount()
    {
        return rowCount;
    }

    /**
     * @param rowCount the rowCount to set
     */
    public void setRowCount(String rowCount)
    {
        this.rowCount = rowCount;
    }

    /**
     * @return the action
     */
    public String getAction()
    {
        return action;
    }

    /**
     * @param action the action to set
     */
    public void setAction(String action)
    {
        this.action = action;
    }

}
